package com.sun.rabbitMQ.HelloWorld;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    private static final String QUEUE_NAME = "hello";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setUsername("guest");
        factory.setPassword("guest");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        //声明 接收消息
        DeliverCallback callback = (consumerTag,message)->{
            System.out.println(new String(message.getBody()));
        };
        //取消消息的回调
        CancelCallback cancelCallback = consumerTag->{
            System.out.println("消费被中断");
        };
        /*
            1 队列名
            2 是否自动应答 true 自动应答  false 手动应答
            3 接收的消息  消费者未成功接收到消息的回调内容
            4 回调内容  消费者取消回调
         */
        channel.basicConsume(QUEUE_NAME,true,callback,cancelCallback);

    }
}
